package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzx.boot.constant.GlobalEnum;
import com.zzx.boot.entity.table.Image;
import com.zzx.boot.entity.table.LoginUser;
import com.zzx.boot.entity.vo.ImageVo;
import com.zzx.boot.entity.vo.SupportVo;
import com.zzx.boot.exception.SystemctlException;
import com.zzx.boot.mapper.UtilsMapper;
import com.zzx.boot.servlet.SupportService;
import com.zzx.boot.servlet.UtilsService;
import com.zzx.boot.utils.DateTImeUtils;
import com.zzx.boot.utils.R;
import com.zzx.boot.utils.SecurityUtils;
import com.zzx.boot.utils.Upload;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/8 17:03
 */
@Service
@RequiredArgsConstructor
public class UtilsServiceImpl implements UtilsService {

    private final UtilsMapper utilsMapper;
    @Resource
    @Lazy
    private SupportService supportService;

    @Override
    public ImageVo upload(MultipartFile file) {
        ImageVo vo = null;
        String filename = Upload.getFilename(file);
        try {
            String url = Upload.sendUpload(file.getInputStream(), filename);
            Image image = new Image(null,url, SecurityUtils.getUserId(),DateTImeUtils.getDate());
            boolean b = utilsMapper.uploadByUserId(image);
            if(!b)throw new SystemctlException(GlobalEnum.IMAGE_UPLOAD_ERROR);
             vo = new ImageVo(image.getId().toString(),url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vo;
    }

    @Override
    public boolean addImageByPost(List<String> images,Long postId) {
        return utilsMapper.addImageByPost(images,DateTImeUtils.getDate(),postId);
    }

    @Override
    public List<Long> getImagesByPostId(Long id) {
        return utilsMapper.getImagesByPostId(id);
    }

    @Override
    public List<Long> getPostIdByTopicId(Page page,Long topicId) {
        return utilsMapper.getPostIdByTopicId(page,topicId);
    }

    @Override
    public Map<Long, SupportVo> getBetchByUser(LoginUser loginUser) {
        return supportService.getBetchByUserId(loginUser);
    }

    @Override
    public SupportVo getByUser(LoginUser loginUser,Long postId) {
        return supportService.getUserId(loginUser,postId);

    }
}
